﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.IO;
using System.Text;
using System.Windows.Forms;
using Tzx.Utilities.Npoi;

namespace Excel2Words
{
    /// <summary>
    /// 
    /// </summary>
    public partial class UcExcelImport : UserControl
    {
        #region public

        /// <summary>
        /// 
        /// </summary>
        public UcExcelImport()
        {
            InitializeComponent();

            LblReadMeExcel.BackColor = AppHelp.Color_Excel;
            LblReadMeExcel.ForeColor = Color.White;

            Load += UcExcelImport_Load;
            BtnSelectExcel.Click += BtnSelectExcel_Click;
            TxtExcelFile.TextChanged += TxtExcelFile_TextChanged;
        }

        /// <summary>
        /// 
        /// </summary>
        public DataTable ExcelTable
        {
            get
            {
                if (_excelTable == null)
                {
                    _excelTable = new DataTable("空表");
                    _excelTable.Columns.Add("空表F1", typeof (string));
                    DataRow dr = _excelTable.NewRow();
                    dr[0] = "#错误!!!空表!!!#";
                    _excelTable.Rows.Add(dr);
                }
                return _excelTable;
            }
        }
        private DataTable _excelTable;

        /// <summary>
        /// 
        /// </summary>
        public event EventHandler ExcelFileChanged;

        #endregion

        #region 事件方法

        /// <summary>
        /// 选定Excel文件变化时
        /// </summary>
        void TxtExcelFile_TextChanged(object sender, EventArgs e)
        {
            string file = TxtExcelFile.Text = TxtExcelFile.Text.Trim();
            if (file.Length > 0)
            {
                bool ok = LoadExcel(file);
                if (!ok)
                {
                    return;
                }

                //显示
                dataGridView1.DataSource = ExcelTable;

                //触发事件
                EventHandler handler = ExcelFileChanged;
                if (handler != null) handler(this, EventArgs.Empty);
            }
        }

        /// <summary>
        /// 选择文件按钮
        /// </summary>
        void BtnSelectExcel_Click(object sender, EventArgs e)
        {
            using (OpenFileDialog ofd = new OpenFileDialog())
            {
                ofd.Title = "选择Excel文件";
                ofd.Filter = "Excel文件(*.xlsx,*.xls)|*.xlsx;*.xls";
                ofd.InitialDirectory = Application.StartupPath;
                if (ofd.ShowDialog() == DialogResult.OK)
                {
                    TxtExcelFile.Text = ofd.FileName;
                }
            }
        }

        /// <summary>
        /// 窗体加载时
        /// </summary>
        void UcExcelImport_Load(object sender, EventArgs e)
        {
            //LblReadMeApp.Text = AppHelp.TEXT_Read_App;
            LblReadMeExcel.Text = AppHelp.TEXT_Read_Excel;
            //LblReadMeWord.Text = AppHelp.TEXT_Read_Word;
            //TxtHelp.Text = AppHelp.TEXT_Read_Help;
        }

        #endregion

        #region private

        /// <summary>
        /// 加载Excel文件
        /// </summary>
        /// <param name="excelFile"></param>
        private bool LoadExcel(string excelFile)
        {
            if (!File.Exists(excelFile))
            {
                MessageBox.Show("Excel文件不存在", AppHelp.Title, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return false;
            }

            //是xls还是xlsx
            bool isExcel2007 = excelFile.EndsWith(".xlsx", true, null);
            bool isExcel97 = excelFile.EndsWith(".xls", true, null);
            if (!isExcel2007 && !isExcel97)
            {
                MessageBox.Show("不是支持的Excel文件(*.xls或*.xlsx)", AppHelp.Title, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return false;
            }

            //
            bool ok = false;
            //try
            //{
                using (FileStream fs = new FileStream(excelFile, FileMode.Open, FileAccess.Read))
                {
                    NpoiImport import = new NpoiImport(fs, isExcel2007);
                    _excelTable = import.ExcelStreamToDataTable(0, 0);
                    fs.Close();
                    ok = true;
                }
            //}
            //catch (Exception ex)
            //{
            //    MessageBox.Show("出错:" + ex.Message, AppHelp.Title, MessageBoxButtons.OK, MessageBoxIcon.Error);
            //}

            //返回
            return ok;
        }

        #endregion

    }
}